package com.shumei.DAO.impl;
import com.shumei.DAO.ProductDAO;
import com.shumei.pojo.Product;
import com.shumei.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ProductDAOImpl implements ProductDAO{
    @Override
    public ArrayList<Product> getProductList() {
        String sql="select * from product";
        ArrayList<Product> productlist=new ArrayList<>();
        Connection conn = null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement(sql);
            rs=ps.executeQuery();
            while(rs.next()){
                int pid=rs.getInt(1);
                String pname=rs.getString(2);
                Double price=rs.getDouble("price");
                int stock=rs.getInt("stock");
                String img=rs.getString("img");
                productlist.add(new Product(pid,pname,price,stock,img));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(conn,ps,rs);
        }
        return productlist;
    }
    public Boolean deleteProduct(int pid) {
        String sql="delete from product where pid=?";
        Connection conn=null;
        PreparedStatement ps=null;
        Boolean flag=false;
        try {
            conn=DBUtil.getConnection();
            ps=conn.prepareStatement(sql);
            ps.setInt(1,pid);
            flag=(ps.executeUpdate()>0);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }
    public Boolean addProduct(Product p) {
        String sql="insert into product(pname,price,stock) values(?,?,?)";
        Connection conn=null;
        PreparedStatement ps=null;
        Boolean flag=false;
        try {
            conn=DBUtil.getConnection();
            ps=conn.prepareStatement(sql);
            ps.setString(1,p.getPname());
            ps.setDouble(2,p.getPrice());
            ps.setInt(3,p.getStock());
            flag=(ps.executeUpdate()>0);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }
    public Boolean updateProduct(Product p) {
        String sql="update product set pname=?,price=?,stock=? where pid=?";
        Connection conn=null;
        PreparedStatement ps=null;
        Boolean flag=false;
        try {
            conn=DBUtil.getConnection();
            ps=conn.prepareStatement(sql);
            ps.setString(1,p.getPname());
            ps.setDouble(2,p.getPrice());
            ps.setInt(3,p.getStock());
            ps.setInt(4,p.getPid());
            flag=(ps.executeUpdate()>0);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }

    @Override
    public Product getProductByPid(int pid) {
        String sql="select * from product where pid=?";
        Connection conn = null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setInt(1,pid);
            rs=ps.executeQuery();
            if(rs.next()){
                String pname=rs.getString(2);
                Double price=rs.getDouble("price");
                int stock=rs.getInt("stock");
                String img=rs.getString("img");
                Product p=new Product(pid,pname,price,stock,img);
                return p;
            }
            else return null;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(conn,ps,rs);
        }
    }

    public Product getProductById(int pid) {
        String sql="select * from product where pid=?";
        Connection conn = null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setInt(1,pid);
            rs=ps.executeQuery();
            if(rs.next()){
                String pname=rs.getString(2);
                Double price=rs.getDouble("price");
                int stock=rs.getInt("stock");
                String img=rs.getString("img");
                Product p=new Product(pid,pname,price,stock,img);
                return p;
            }
            else return null;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(conn,ps,rs);
        }
    }
}
